#/usr/bin/perl
#
#
#

use strict;


my $file = 'data.txt';


open F, "<$file";

my $last;
my $parentName = "";
my @parentsCode = (86);
my $lastLevel = 0;
my %pl;
my $idx = 0;
print "86\t中国\t0\t-1\t0\n";
$idx = 1;
$pl{86} = 0;
while(<F>) {
	chomp;
	next if /^#/;
	next if /^\s*$/;
	my ($code, $level, $name) = split ",";

	if($level == 4) {$level = 3;}
	my $parentCode = $parentsCode[$level -1];
	my $pos = $pl{$parentCode};
	print "$code\t$name\t$parentCode\t$pos\t$level\n";

	#check level and code
	{
		if($parentCode != 86) {
			my $p = substr($parentCode, 0, ($level - 1) * 2);
			my $c = substr($code, 0, ($level - 1) * 2);
			if($p != $c) {
				print STDERR "$parentCode, $parentName, $code, $name, $level\n";
			}
		}
	}

	$parentsCode[$level] = $code;
	$pl{$code} = $idx++;
}
